package com.meme.editor.level.define;

import org.apache.log4j.Logger;

import com.meme.editor.gui.sprite2d.Sprite2dTreeView;

public class Parameter 
{
	static Logger logger = Logger.getLogger (Parameter.class);

	String mName;
	Object mValue;
	long mNumberValue;
	boolean mNumberValueParsed;
	
	public Parameter (String name, String value)
	{
		mName  = name;
		mValue = value;
		mNumberValueParsed = false;
	}
	
	public String getName ()
	{
		return mName;
	}
	
	public Object getValue ()
	{
		return mValue;
	}
	
	public String getStringValue ()
	{
		return mValue.toString();
	}
	
	public byte getByteValue ()
	{
		parseNumberValue ();
		return (byte) mNumberValue;
	}
	
	public short getShortValue ()
	{
		parseNumberValue ();
		return (short) mNumberValue;
	}
	
	public int getIntValue ()
	{
		parseNumberValue ();
		return (int) mNumberValue;
	}
	
	public long getLongValue ()
	{
		parseNumberValue ();
		return mNumberValue;
	}
	
	void parseNumberValue ()
	{
		if (mNumberValueParsed)
			return;
		
		String str = getStringValue();
		
		if (str == null)
		{
			mNumberValue = 0;
		}
		else
		{
			try
			{
				if (str.startsWith ("0x") || str.startsWith ("0X"))
					mNumberValue = Long.parseLong (str.substring(2), 16);
				else
					mNumberValue = Long.parseLong (str);
			}
			catch (Exception e)
			{
				logger.error ("parseLong error: " + str);
				e.printStackTrace();
			}
		}	
		
		mNumberValueParsed = true;
	}
	
	
	@Override
	public String toString ()
	{
		return mName;
	}
}
